package com.project.service.impl;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.mapper.LogMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/***
 * 用户业务实现类
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
     private IUserMapper userMappers;
    @Autowired
     private LogMapper logMapper;
     public static LogBean log = new LogBean();
     public static LocalDate time = log.getDate();
    @Override
    public UserBean logUser(String name, String pwd) {
        if(userMappers.logUser(name, pwd) == null ){
            return null;
        }
        else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
            Date date = new Date(System.currentTimeMillis());
            String str2 = date.toString().replaceAll(" ", "");

            int x = str2.indexOf("9");
            int y = str2.indexOf("C");
            String times = str2.substring(x + 1, y);

            String info = "用户" + userMappers.logUser(name, pwd).getName() + "在" + times + "登陆系统";
            logMapper.addLog(time, info);
        }
        return userMappers.logUser(name, pwd);
    }

    @Override
    public void addUser(UserBean user) {
       userMappers.addUser(user);
    }

    @Override
    public void delUser(int id) {
           userMappers.delUser(id);
    }

    @Override
    public void updatePwd(int id, String pwd) {
            userMappers.updatePwd(id,pwd);
    }

    @Override
    public void updateUser(int id, String grade) {
       userMappers.updateUser(id,grade);
    }

    @Override
    public UserBean findById(int id) {

        return userMappers.findById(id);
    }

    @Override
    public List<UserBean> findByGrade(String grade) {
        return userMappers.findByGrade(grade);
    }
}
